package com.xyq.fs.index;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.TextField;

import org.apache.lucene.document.Field.Store;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.xyq.fs.base.MyDirectoryS;
import com.xyq.fs.constants.FieldConstant;
import com.xyq.fs.contants.filesuffixes2.ExcelEnums;

public class IndexExcelFiles implements IndexFileInf {

	@Override
	public void indexFile(Path path, String bz) {
		ExcelTypeEnum en = null;
		Document doc = null;
		String ps = path.toString().toUpperCase();
		if (ps.endsWith(ExcelEnums.XLS)) {
			doc = getSimDoc(path, ExcelEnums.XLS, bz);
			en = ExcelTypeEnum.XLS;
		} else if (ps.endsWith(ExcelEnums.XLSX)) {
			en = ExcelTypeEnum.XLSX;
			doc = getSimDoc(path, ExcelEnums.XLSX, bz);
		} else if (ps.endsWith(ExcelEnums.ET)) {
			doc = getSimDoc(path, ExcelEnums.ET, bz);
			en = ExcelTypeEnum.XLS;
		}
		ps = getContent2(path, en);
		TextField tx = new TextField(FieldConstant.FILE_CONTENT, ps, Store.NO);
		doc.add(tx);
		MyDirectoryS.addDocument(doc);

		doc.clear();
		doc = null;
		tx = null;
		ps = null;
		path = null;
		bz = null;
	}

	public String getContent2(Path path, ExcelTypeEnum en) {

		if (en == null) {
			String ps = path.toString().toUpperCase();
			if (ps.endsWith(ExcelEnums.XLS)) {

				en = ExcelTypeEnum.XLS;
			} else if (ps.endsWith(ExcelEnums.XLSX)) {
				en = ExcelTypeEnum.XLSX;

			} else if (ps.endsWith(ExcelEnums.ET)) {

				en = ExcelTypeEnum.XLS;
			}
		}
		StringBuilder sb = new StringBuilder();
		try (InputStream inputStream = new FileInputStream(path.toFile());) {

			@SuppressWarnings("deprecation")
			ExcelReader reader = new ExcelReader(inputStream, en, null, new AnalysisEventListener<List<String>>() {
				@Override
				public void invoke(List<String> object, AnalysisContext context) {

					if (object.size() > 0) {
						for (String s : object) {
							if (s != null && s.trim().length() > 0)
								sb.append(s).append("\t");
						}
						sb.append("\n");
					}
					object.clear();
					object = null;
					context = null;
				}

				@Override
				public void doAfterAllAnalysed(AnalysisContext context) {
					context = null;
				}
			});
			reader.read();

		} catch (Exception e) {

			System.out.println("无法解析" + path);
		}
		return sb + "";
	}

	public static void main(String[] args) throws IOException {

		// IndexExcelFiles ie = new IndexExcelFiles();
		// String s = ie.getContent2(Paths.get("f:\\最后5000.csv"));
		// System.out.println(s);
		Files.readAllLines(Paths.get("f:\\最后5000.csv")).forEach(s -> {
			System.out.println(s);
		});
		;

	}
}
